package com.training1.core.web.controller;


import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.training1.core.common.controller.BaseController;
import com.training1.core.common.dao.Page;
import com.training1.core.common.entity.RespJson;
import com.training1.core.common.utils.framework.RespJsonFactory;
import com.training1.core.entity.BranchEntity;
import com.training1.core.modules.service.BranchService;

@Controller
@RequestMapping("branch")
public class BranchController extends BaseController{
	
	
	@Resource
	private BranchService branchService;
	
	/**
	 * 进入分支结构
	 * @return
	 */
	@RequestMapping(value={"","/"})
	public String go(){
		return "branch/branch";
	}
	
	/**
	 * 查询所有数据
	 * @param title
	 * @param keyWord
	 * @param isValid
	 * @param page
	 * @return
	 */
	@RequestMapping(value="list",method=RequestMethod.GET)
	@ResponseBody
	public Page<BranchEntity> list (String id,String phone,String orgName, Page<BranchEntity> page){
	
		page.getSearch().put("id", id);
		page.getSearch().put("phone", phone);
		page.getSearch().put("orgName", orgName);
		branchService.findListById(page.getSearch());
		
		return page;
		
	}
	
	/**
	 * 查询所有的分支机构类型
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/branchType")
	@ResponseBody
	public List<BranchEntity> findAllCompany(){
		
		List<BranchEntity> branchTypes	= branchService.findAllTypeName();
		
		
		return branchTypes;
	}
	
	/**
	 * 删除
	 * @param help
	 * @return
	 */
	@RequestMapping(value="delete", method={RequestMethod.DELETE})
	@ResponseBody
	public RespJson delByid (@RequestBody BranchEntity branch ){
		
		branchService.delete(branch.getId());
		
		return RespJsonFactory.buildSuccess();
		
	}
	
	
	/**
	 * 保存
	 * @param 
	 * @return
	 */
	@RequestMapping(value="save", method={RequestMethod.POST})
	@ResponseBody
	public RespJson save (@Valid BranchEntity branch ){
		
	String result = branchService.checkOrgType();
		if(result.equals("0")&& branch.getOrgType().equals("0")){
			return RespJsonFactory.buildFailure();
		}
		branchService.save(branch);
		
		return RespJsonFactory.buildSuccess();
	}
	
	@RequestMapping(value="edit")
	@ResponseBody
	public BranchEntity edit (String id ){
		
		BranchEntity branch	= branchService.get(Integer.valueOf(id));
		
		return branch;
	}
	/**
	 * 检查是否有重复的机构名称
	 * @param orgName
	 * @return
	 */
	@RequestMapping(value="checkOrgName")
	@ResponseBody
	public String checkOrgName (String orgName ){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("orgName", orgName);
		
		
		String result= branchService.checkOrgName(map);
		
		return result;
	}
	
	
	
}
